package com.mbiolance.cloud.activiti.restful;

import com.mbiolance.cloud.activiti.domain.dto.ProcessInstanceDto;
import com.mbiolance.cloud.activiti.domain.vo.StartProcessVo;
import com.mbiolance.cloud.activiti.rpc.ProcessInstanceFeignService;
import com.mbiolance.cloud.activiti.service.ProcessInstanceService;
import com.mbiolance.cloud.auth.domain.PageModel;
import com.mbiolance.cloud.auth.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.impl.task.TaskDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "流程实例相关接口")
@RestController
@Slf4j
@RequestMapping("process-instance")
public class ProcessInstanceRestful implements ProcessInstanceFeignService {

    @Autowired
    private ProcessInstanceService processInstanceService;

    @ApiOperation("获取流程实例的分页列表")
    @GetMapping("page")
    public R<PageModel<ProcessInstanceDto>> page(@RequestParam(required = false, defaultValue = "1") Integer number
            , @RequestParam(required = false, defaultValue = "10") Integer size) {
        PageModel<ProcessInstanceDto> page = processInstanceService.page(number, size);
        return R.ok(page);
    }

    @ApiOperation("获取历史流程实例的分页列表")
    @GetMapping("history-page")
    public R<PageModel<ProcessInstanceDto>> historyPage(Integer number, Integer size) {
        PageModel<ProcessInstanceDto> page = processInstanceService.historyPage(number, size);
        return R.ok(page);
    }

    @Override
    public R<String> start(StartProcessVo vo) {
        String start = processInstanceService.start(vo);
        return R.ok(start);
    }

    @Override
    public R<List<ProcessInstanceDto>> listByIds(List<String> processInstanceIds) {
        List<ProcessInstanceDto> processInstanceDtos = processInstanceService.listByIds(processInstanceIds);
        return R.ok(processInstanceDtos);
    }

    @GetMapping("next-tasks/{processInstanceId}")
    public R<Void> nextTasks(@PathVariable String processInstanceId) {
        TaskDefinition nextTaskInfo = processInstanceService.getNextTaskInfo(processInstanceId);

        System.out.println();

        return R.ok();
    }

    @Override
    public R<Boolean> claim(String id) {
        return R.ok(processInstanceService.claim(id));
    }

    @Override
    public R<Boolean> retTask(String id) {
        return R.ok(processInstanceService.retTask(id));
    }

    @Override
    public R<Void> completeTask(String id, Map<String, Object> variables) {
        processInstanceService.completeTask(id, variables);
        return R.ok();
    }

}
